package com.travel.travel_springboot_web.mapper;

import com.travel.travel_springboot_web.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {
    @Insert("INSERT INTO goods (title, `desc`, cover_image, price, stock, gift_rules, " +
            "status, sales, created_at, updated_at) " +
            "VALUES (#{title}, #{desc}, #{coverImage}, #{price}, #{stock}, " +
            "#{giftRules,typeHandler=com.travel.travel_springboot_web.handler.GiftRuleListTypeHandler}, " +
            "#{status}, COALESCE(#{sales}, 0), NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Goods goods);
    
    @Select("<script>" +
            "SELECT * FROM goods " +
            "WHERE status = 1 " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "   AND title LIKE CONCAT('%', #{keyword}, '%') " +
            "</if>" +
            "ORDER BY created_at DESC" +
            "</script>")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "title", column = "title"),
        @Result(property = "coverImage", column = "cover_image"),
        @Result(property = "price", column = "price"),
        @Result(property = "stock", column = "stock"),
        @Result(property = "giftRules", column = "gift_rules", 
            typeHandler = com.travel.travel_springboot_web.handler.GiftRuleListTypeHandler.class),
        @Result(property = "status", column = "status"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at")
    })
    List<Goods> findAll(@Param("keyword") String keyword);
    
    // 物理删除商品
    @Delete("DELETE FROM goods WHERE id = #{id}")
    int deleteById(Long id);
    
    // 逻辑删除商品（修改状态为0）
    @Update("UPDATE goods SET status = 0, updated_at = NOW() WHERE id = #{id}")
    int softDeleteById(Long id);
    
    // 检查商品是否存在
    @Select("SELECT COUNT(*) FROM goods WHERE id = #{id}")
    int checkExists(Long id);
    
    @Update("UPDATE goods SET title = #{title}, `desc` = #{desc}, " +
            "cover_image = #{coverImage}, price = #{price}, stock = #{stock}, " +
            "gift_rules = #{giftRules,typeHandler=com.travel.travel_springboot_web.handler.GiftRuleListTypeHandler}, " +
            "status = #{status}, updated_at = NOW() " +
            "WHERE id = #{id}")
    int update(Goods goods);
    
    @Select("SELECT * FROM goods WHERE id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "title", column = "title"),
        @Result(property = "desc", column = "desc"),
        @Result(property = "coverImage", column = "cover_image"),
        @Result(property = "price", column = "price"),
        @Result(property = "stock", column = "stock"),
        @Result(property = "giftRules", column = "gift_rules", 
            typeHandler = com.travel.travel_springboot_web.handler.GiftRuleListTypeHandler.class),
        @Result(property = "sales", column = "sales"),
        @Result(property = "status", column = "status"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at"),
        @Result(property = "imageUrls", column = "imageUrls", javaType = List.class,
            typeHandler = com.travel.travel_springboot_web.handler.StringListTypeHandler.class)
    })
    Goods findById(Long id);
    
    @Update("UPDATE goods SET stock = stock + #{num}, " +
            "updated_at = NOW() " +
            "WHERE id = #{id} AND status = 1")
    boolean increaseStock(@Param("id") Long id, @Param("num") Integer num);
    
    @Update("UPDATE goods SET stock = stock - #{num}, " +
            "updated_at = NOW() " +
            "WHERE id = #{id} AND status = 1 AND stock >= #{num}")
    boolean decreaseStock(@Param("id") Long id, @Param("num") Integer num);
} 